import tool from '../tool'
import util from '_util/index'
import logic from '_logic'
const store = util.store
import _ from 'lodash'

export default{
  get_type_auth(_this,type){
      
      let auths=this.get_user_auth(_this,_this.ctl)||[];
      if(auths&&util.isArray(auths)&&auths.length>0) {
          auths = auths.filter(auth => [1,3].contains(auth.type));

          let opArr=null;
          switch (type) {
              case 'op':
                  opArr = ['timeSearch', 'delete', 'onOff', 'add', 'search', 'export'];
                  break;
              case 'cell_op':
                  break;
              case 'menu':
                  break;
          }
          // if(opArr)
          //     auths = auths.filter(auth => opArr.contains(auth.flag));

          auths=auths.map(item=> {
              return item.flag;
          })
      }
      auths=_.reduce(auths, (rs, val, ind) => {
          let tmp = val.split('|');
          rs = _.concat(rs,tmp);
          return rs;
      },[]);
      return _.uniq(auths);
  },
  get_user_auth(_this,ctl=null){
      if(!util.is_admin(_this)) {

          let auths= util.store.get(_this, 'auth.auths', 'user');
          let rs = ctl ? auths[ctl] : auths;
          return rs;
      }else{
          return null;
      }
  },
  get_user_citys(_this){
      if(!util.is_admin(_this)) {

          let citys= util.store.get(_this, 'auth.citys', 'user');
          if(!citys)
              return null;
          return citys.contains(-1)?-1:citys.join(',');
      }else{
          return null;
      }
  },
  set_user_auth(_this,clb=null){

      if(!util.is_admin(_this)) {
          _this.apiState4 = false;
          let url = tool.getApi(_this, 'Authority','getUserAuth');

          let param = {
          };
          util.http.SR(_this, url, param, null, (rs) => {
              let ret=false;
              if(rs.menu_auths&&rs.auths) {
                  util.store.push(_this, 'setAuth', 'auth', false, rs, 'user');
                  util.store.commit(_this,'set_auth_ctls',{},'user');
                  ret=true;
                  _this.apiState4 = true;
              }
              if(clb)
                clb(ret);

          });

      }else{
          util.store.push(_this, 'setAuth', 'auth', false, {}, 'user');
          if(clb)
            clb(true);
          _this.apiState4 = true;
      }

  },
    set_auth_ctls(_this){
        let menu_auths= util.store.get(_this, 'auth.menu_auths', 'user');
        if(!menu_auths) {
            return null;
        }
        let attr = [];
        menu_auths.forEach(menu_auth => {
            // return menu_auth.menu_group;
            let rs=menu_auth.menu_group;
            // tool.cl('menu_auth.menu_group:',menu_auth.menu_group);
            if(rs)
                util.forEach(rs,(ind,item) => {
                    item && attr.push(JSON.parse(item.attr_group_str));
                });

        });

        attr=_.reduce(attr, function(result, item) {
            result = _.concat(result,_.flattenDeep(item));
            return _.uniq(result);
        }, []);
        tool.cl('attr2:',attr);

        util.store.push(_this, 'setAuth', 'auth_ctls', false, attr, 'user');
        return attr;
    },
    get_auth_menu(_this,mid){
        let menu_auths= util.store.get(_this, 'auth.menu_auths', 'user');
        if(!menu_auths) {
            return null;
        }
        if(mid) {
            // tool.cl('menu_auths:',menu_auths);
            let menu_groups = menu_auths.map(menu_auth => {
                return menu_auth.menu_group[mid];
            }).filter(menu_auth =>menu_auth);
            // tool.cl('mid:',mid);
            // tool.cl('menu_groups:',menu_groups);
            menu_groups=this._hl_auth_menu_groups(menu_groups);
            // tool.cl('menu_groups:',menu_groups);
            return menu_groups;
        }else{
            let top_menus = menu_auths.map(menu_auth => {
                return menu_auth.top_menu;
            });
            top_menus=this._hl_auth_top_menus(top_menus);
            // tool.cl('top_menus:',top_menus);
            return top_menus;
        }

    },
    _hl_auth_menu_groups(menu_groups){
        let attr = [];
        let group = [];
        menu_groups.forEach(item => {
            // tool.cl('item:',item);
            attr.push(JSON.parse(item.attr_group_str));
            group.push(JSON.parse(item.group_str));
        });

        attr=_.reduce(attr, function(result, item) {
            result = _.concat(result,_.flattenDeep(item));
            return _.uniq(result);
        }, []);
        group=_.reduce(group, function(result, item) {
            result = _.concat(result,_.flattenDeep(item));
            return _.uniq(result);
        }, []);
        menu_groups = {attr, group};
        return menu_groups;
    },
    _hl_auth_top_menus(top_menus){
        top_menus=top_menus.map(item => {

            item=_.reduce(item, (rs, val,key) => {
                rs[val.id] = val.disable || false;
                return rs;
            },{});
            return item;
        });
        top_menus=_.reduce(top_menus, (rs, val,key) => {
            rs=_.mergeWith(rs, val, (a,b)=> {
                // tool.cl('a,b:', a, b);
                let rs=  a!==undefined?a&&b:b;
                // tool.cl('rs:', rs);
                return rs;
            });
            return rs;
        },{});

        let out = [];
        util.forEach(top_menus, (id, disable) => {
            out.push({
                id, disable
            });
        });

        return out;
    }
}
